<?php
/**
 ***** 扶摇而上、抱守本真 ******
 * @author Dana
 * @Date 2021/6/21
 */

/**
 * Class address
 * @question 希望复制一个对象
 * @programme 使用=按引用复制对象 OR 使用 clone 按值复制对象
 * @author Zhoufurong
 * @Date 2021/6/21
 */

class address{

    protected $city;
    protected $country;

    /**
     * @param mixed $city
     */
    public function setCity($city): void
    {
        $this->city = $city;
    }

    /**
     * @return mixed
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * @return mixed
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * @param mixed $country
     */
    public function setCountry($country): void
    {
        $this->country = $country;
    }
}

class Persons{

    protected $name;
    protected $address;

    public function __construct()
    {
        $this->address = new Address;
    }

    public function __clone()
    {
        $this->address = clone $this->address;
    }


    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param mixed $name
     */
    public function setName($name): void
    {
        $this->name = $name;
    }

    public function __call($method, $arguments)
    {
        if(method_exists($this->address, $method)){
            return call_user_func_array(array($this->address, $method),$arguments);
        }

    }
}


$rasmus = new Persons;
$rasmus->setName('Rasmus Lerdorf');
$rasmus->setCity('Sunnyvale');

$zeev = clone $rasmus;
$zeev->setName('Zeev Suraski');
$zeev->setCity('Tel Aviv');

print $rasmus->getName(). ' lives in '.$rasmus->getCity().'.';
print "\n";
print $zeev->getName(). ' lives in '.$zeev->getCity().'.';

